﻿using System;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Core.DAL;
using Medianamik.Core.DAL.Repositories;
using Medianamik.UI.Web.Shop.Data;
using Medianamik.UI.Web.Shop.Data.Interfaces;
using Medianamik.UI.Web.Shop.Data.Repositories;
using Medianamik.UI.Web.MVP.Presentation;
using SubSonic;
using Medianamik.Core.Sugar;

namespace Medianamik.UI.Web.Shop.Presentation.Presenters
{
    public class OrderListPresenter<T, TItem, TView> : ModulePresenterWithGrid<T, TView,
        IOrderRepository<T, TItem>, INodeTypeRepository, IMedianamikRepository<IMedianamikDTOWithName>>
        where T : class, IOrder<TItem>, new() 
        where TItem : Data.Interfaces.IOrderItem
        where TView : class, IOrders
    {
        public OrderListPresenter(TView view, IOrderRepository<T, TItem> orderRepository,
            INodeTypeRepository nodeTypeRepository, IMedianamikRepository<IMedianamikDTOWithName> baseTypeRepository)
            : base(view, orderRepository, nodeTypeRepository, baseTypeRepository)
        {

        }

        private TView ShopView
        {
            get { return View; }
        }

        protected void AddAndToQuery(SqlQuery query)
       {
           if (!View.SelectedDateColumnName.IsNullOrEmpty())
           {
               if (View.StartingDate != null)
               {
                   query.And(View.SelectedDateColumnName).IsGreaterThanOrEqualTo(
                    View.StartingDate.Value.ToStartingComparedDate());
               }

               if (View.EndingDate != null)
               {
                   query.And(View.SelectedDateColumnName).IsLessThanOrEqualTo(
                    View.EndingDate.Value.ToEndingComparedDate());
               }
           }

           if (!View.TransactionId.IsNullOrEmpty())
           {
               query.And(ShopSchema.Order.Columns.TransactionId).Like(String.Format("%{0}%", View.TransactionId));
           }

           if(!View.ClientEmail.IsNullOrEmpty())
           {
               query.And(ShopSchema.ClientProfile.Columns.Email).Like(String.Format("%{0}%", View.ClientEmail));
           }

           if (!View.ClientName.IsNullOrEmpty())
           {
               query.And(ShopSchema.ClientProfile.Columns.Name).Like(String.Format("%{0}%", View.ClientName));
           }

           if (!View.ClientPhoneNumber.IsNullOrEmpty())
           {
               query.And(ShopSchema.ClientProfile.Columns.PhoneNumber).Like(String.Format("%{0}%", View.ClientPhoneNumber));
           }

           if (Convert.ToInt32(View.SelectedStatus) != 2)
           {
               query.And(ShopSchema.Order.Columns.IsActive).IsEqualTo(Convert.ToInt32(View.SelectedStatus)-1);
           }
       }

        protected override void SetCustomGridColumns(Telerik.Web.UI.GridItem item, T element, bool existInCurrentCulture)
        {
            base.SetCustomGridColumns(item, element, existInCurrentCulture);

            SetLiteralGridItem("TransactionIdGridItem", item, element.TransactionId);
            SetLiteralGridItem("PurchaseDateGridItem", item, element.PurchaseDate.ToString());
            SetLiteralGridItem("ShippedDateGridItem", item, element.ShippedDate.ToString());
        }

        private static void SetLiteralGridItem(string literalName, Telerik.Web.UI.GridItem item, string value)
        {
            var literal = item.FindControl(literalName) as Literal;
            literal.Text = value;
        }
    }

    public class OrderListPresenter : OrderListPresenter<Order, OrderItem, IOrders>
    {
        public OrderListPresenter(IOrders view, IOrderRepository<Order, OrderItem> orderRepository,
            INodeTypeRepository nodeTypeRepository, IMedianamikRepository<IMedianamikDTOWithName> baseTypeRepository) :
            base(view, orderRepository, nodeTypeRepository, baseTypeRepository)
        {
        }

        protected override PagedCollection<Order> GetData()
        {
            var count = 0;
            var query = new Select()
                .From(ShopSchema.Order.TableName)
                .InnerJoin(ShopSchema.ClientProfile.TableName, ShopSchema.BaseColumns.NodeId,
                           ShopSchema.Order.TableName, ShopSchema.Order.Columns.ClientProfile)
                .Where("Generation").IsEqualTo(ContentState.Draft)
                .And(ClientProfile.Schema.GetColumn(ClientProfile.Columns.Generation)).IsEqualTo(ContentState.Draft);

            AddAndToQuery(query);
            count = query.GetRecordCount();
            query.Paged(PageIndex, PageSize);
            query.OrderAsc(ShopSchema.Order.Columns.PurchaseDate);

            return new PagedCollection<Order>(query.ExecuteTypedList<Order>(), count);
        }
    }
}
